/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class ExpertoXml extends DefaultHandler {

    private final XMLReader xr;
    List<DtoVariable> lista= new ArrayList<DtoVariable>();
    String texto = new String();
    String textoAux="listablanca";
    private List<DtoMensaje> listaMsjs;
    DtoMensaje dtoMsjAux;
    

    public ExpertoXml() throws SAXException {
        xr = XMLReaderFactory.createXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        
    }

    
    public List<DtoMensaje> leerVariables(final String archivoXML) throws FileNotFoundException, IOException, SAXException {
        listaMsjs=new ArrayList<DtoMensaje>();
        FileReader fr = new FileReader(archivoXML);
        //SuscriptorLog.getInst().notificar("Mail validos:");
        xr.parse(new InputSource(fr));


        return listaMsjs;
    }

    @Override
    public void startElement(String uri, String name, String qName, Attributes atts) {
        textoAux = qName;
            //if(!qName.equals("listablanca"))SuscriptorLog.getInst().notificar("Nombre del equipo:"+qName);
    }

    @Override
    public void endElement(String uri,String localName,String qName){
        if(qName.equals("medicion")){
            listaMsjs.add(dtoMsjAux);
        }
    }

    @Override
    public void characters(char[] ch, int start, int lenght) {
        String var;

        if(textoAux.equals("medicion")){
            dtoMsjAux=new DtoMensaje("", new ArrayList<DtoVariable>());
        if (textoAux.equals("TimeStamp")) {
             var = new String(ch, start, lenght);
            if (!var.equals("\n")) {
                dtoMsjAux.getVars().add(new DtoVariable("TimeStamp", var));
            }
        }else if(textoAux.equals("Temperatura")){
             var = new String(ch, start, lenght);
            if (!var.equals("\n")) {
                dtoMsjAux.getVars().add(new DtoVariable("Temperatura", var));
            }
        }else if(textoAux.equals("Tension")){
             var = new String(ch, start, lenght);
            if (!var.equals("\n")) {
                dtoMsjAux.getVars().add(new DtoVariable("Tension", var));
            }
        }else if(textoAux.equals("Corriente")){
             var = new String(ch, start, lenght);
            if (!var.equals("\n")) {
                dtoMsjAux.getVars().add(new DtoVariable("Corriente", var));
            }
        }
        else if(textoAux.equals("Potencia")){
             var = new String(ch, start, lenght);
            if (!var.equals("\n")) {
                dtoMsjAux.getVars().add(new DtoVariable("Potencia", var));
            }
        }
        else if(textoAux.equals("Presion")){
             var = new String(ch, start, lenght);
            if (!var.equals("\n")) {
                dtoMsjAux.getVars().add(new DtoVariable("Presion", var));
            }
        }
        
        }

    }

            
}